/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.core;

import java.util.ArrayList;

import com.bluestone.util.Util;

/**
 * A input is corresponse to a testSuite which containts testCases and functions
 * or corresponse to module which contains a set of the pages and the elements.
 * 
 * @author <a href="mailto:bluestone.daniel.q@gmail.com">daniel.q</a>
 */
public class Input {
	private String file;

	private Object obj;

	public String getFile() {
		return file;
	}

	public void setFile(String file) {
		this.file = file;
	}

	public Object getObject() {
		return obj;
	}

	public void setObject(Object obj) {
		this.obj = obj;
	}

	@Override
	public String toString() {
		String str = "input:";
		if (file != null) {
			str = str + "file=" + file;
		}
		return (str);
	}

	/**
	 * According the path to find the page.
	 * 
	 * @param path
	 * @return
	 */
	public Page searchPage(String path) {
		Page page = null;
		if (obj != null && Module.class.isInstance(obj)) {
			Module module = (Module) obj;
			ArrayList pages = module.getPages();

			for (int i = 0; i < pages.size(); i++) {
				Page temp = (Page) pages.get(i);
				if (path.equalsIgnoreCase(temp.getSearchPath())) {
					String owner = temp.getOwner();
					boolean isfind = false;
					if (owner == null) {
						page = temp;
					} else {					
						String[] values = owner.split(",");
						String property = Util.getCurrentProject()
								.getProperty().getValue();
						if (property == null) {
							property = Util.getCurrentProject().getBrowser()
									.getType();
						}
						for (int j = 0; j < values.length; j++) {
							String str = values[j];
							if (property.toUpperCase().indexOf(
									str.toUpperCase()) >= 0) {
								isfind = true;
								return temp;
							}
						}
					}
				}
			}
		}

		return page;
	}

	/**
	 * According the path to find the Function.
	 * 
	 * @param path
	 * @return
	 */
	public Function searchFunction(String path) {
		if (obj != null && Library.class.isInstance(obj)) {
			Library library = (Library) obj;

			ArrayList list = library.getFunctions();
			for (int i = 0; i < list.size(); i++) {
				Function function = (Function) list.get(i);
				if (function.getSearchPath().equalsIgnoreCase(path)) {
					return function;
				}
			}
		}
		return null;
	}

	/**
	 * According the path to find the Element.
	 * 
	 * @param path
	 * @return
	 */
	public Element searchElement(String path) {
		Element element = null;
		Page page = null;
		String pagePath = null;
		String elementPath = null;
		if (path.indexOf("|") > 0) {
			pagePath = path.substring(0, path.lastIndexOf("|"));
			elementPath = path;
		}

		if (pagePath != null) {
			page = searchPage(pagePath);
		} else {
			page = Util.getCurrentPage();
			elementPath = page.getSearchPath() + "|" + path;
		}
		if (page != null) {
			ArrayList elements = page.getElements();
			for (int j = 0; j < elements.size(); j++) {
				Element temp = (Element) elements.get(j);
				if (elementPath.equalsIgnoreCase(temp.getSearchPath())) {
					return temp;
				}
			}
		}
		return element;
	}

	/**
	 * According the path to find the TestCase.
	 * 
	 * @param path
	 * @return
	 */
	public TestCase searchTestCase(String path) {
		if (obj != null && TestSuite.class.isInstance(obj)) {
			TestSuite testsuite = (TestSuite) obj;

			ArrayList list = testsuite.getTestcaseList();
			for (int i = 0; i < list.size(); i++) {
				TestCase testcase = (TestCase) list.get(i);
				if (testcase.getSearchPath().equalsIgnoreCase(path)) {
					return testcase;
				}
			}
		}
		return null;
	}

	/**
	 * According the path to find the Command.
	 * 
	 * @param path
	 * @return
	 */
	public Command searchCommand(String path) {
		if (obj != null && TestSuite.class.isInstance(obj)) {
			TestSuite testsuite = (TestSuite) obj;
			ArrayList list = testsuite.getTestcaseList();
			for (int i = 0; i < list.size(); i++) {
				TestCase testcase = (TestCase) list.get(i);
				Command command = testcase.searchCommand(path);
				if (command != null) {
					return command;
				}
			}
		}
		return null;
	}

}
